package com.tinyseed.emcp.api.basicdata.service;

import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.tinyseed.emcp.api.basicdata.domain.Organize;
import com.tinyseed.emcp.api.basicdata.domain.VenterpriseDeviceStatistics;
import com.tinyseed.emcp.api.basicdata.domain.VrealtimeMonitor;
import com.tinyseed.emcp.api.basicdata.model.OrganizeOutputDTO;
import com.tinyseed.emcp.api.basicdata.model.VenterpriseDeviceStatisticsOutput;
import com.tinyseed.emcp.api.basicdata.model.VrealtimeMonitorInputDTO;
import com.tinyseed.emcp.api.basicdata.model.VrealtimeMonitorOutputDTO;
import com.tinyseed.emcp.api.basicdata.repository.OrganizeRepository;
import com.tinyseed.emcp.api.basicdata.repository.VenterpriseDeviceStatisticsRepository;
import com.tinyseed.emcp.api.basicdata.repository.VrealtimeMonitorRepository;
import com.tinyseed.hmxx.admin.security.utils.SecurityUtils;
import com.tinyseed.hmxx.admin.system.domain.dto.UserDTO;
import com.tinyseed.hmxx.admin.system.service.UserService;
import com.tinyseed.hmxx.common.utils.QueryHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/***
 * @ClassName: VrealtimeMonitorServiceImpl
 * @Description: TODO
 * @Auther: Harlan.Xiong
 * @Date: 2019-12-23 19:03
 * @version : V1.0
 */

@Service
@Slf4j
public class VrealtimeMonitorServiceImpl implements VrealtimeMonitorService {

    @Autowired
    private VrealtimeMonitorRepository vrealtimeMonitorRepository;
    @Autowired
    private VenterpriseDeviceStatisticsRepository venterpriseDeviceStatisticsRepository;
    @Autowired
    private OrganizeRepository organizeRepository;

    @Autowired
    private OrganizeService organizeService;

    @Autowired
    private UserService userService;

    @Override
    public Page page(VrealtimeMonitorInputDTO inputDTO, int size, int current) {
        //过滤行政区域

        Sort sort = Sort.by(Sort.Direction.DESC,"runDeviceCount");
        getInputDTO(inputDTO);
        Pageable pageable = PageRequest.of(current - 1, size,sort);
        Page page = vrealtimeMonitorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, inputDTO, criteriaBuilder), pageable);


        return page;
    }

    @Override
    public Page page(VrealtimeMonitorInputDTO inputDTO, int size, int current, Long deptId) {
        //根据情况查找


        return null;
    }

    @Override
    public List<VrealtimeMonitorOutputDTO> getPoint(VrealtimeMonitorInputDTO inputDTO) {
        getInputDTO(inputDTO);
        List<VrealtimeMonitor> list = vrealtimeMonitorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, inputDTO, criteriaBuilder));
        List<VrealtimeMonitorOutputDTO> outputDTOList = Lists.newArrayList();
        list.forEach(m -> {
            VrealtimeMonitorOutputDTO dto = new VrealtimeMonitorOutputDTO();
            BeanUtils.copyProperties(m, dto);
            outputDTOList.add(dto);
        });
        return outputDTOList;
    }

    private VrealtimeMonitorInputDTO getInputDTO(VrealtimeMonitorInputDTO inputDTO) {
        //查找部门
        UserDTO user = userService.findByName(SecurityUtils.getUsername());
        Long deptId = user.getDept().getId();
        //该部门拥有的权限
        List<OrganizeOutputDTO> organizeOutputDTOS = organizeService.findOrganzeBydept(deptId);
        List<OrganizeOutputDTO> children = new ArrayList<>();

        //找到子权限
        organizeOutputDTOS.forEach(item->{
            children.addAll( organizeService.getOptionList(item.getId()));
        });
        organizeOutputDTOS.addAll(children);
        List<Long> organizeIds;
        organizeIds = organizeOutputDTOS.stream().map(OrganizeOutputDTO::getId).collect(Collectors.toList());


        if (inputDTO.getOrganizeId() != null&& organizeIds.contains(inputDTO.getOrganizeId())) {

            //查找parentId
            List<Organize> organizeIdList = organizeRepository.findAllByParentId(inputDTO.getOrganizeId());
            if (!CollectionUtils.isEmpty(organizeIdList)) {
                organizeIds = organizeIdList.stream().map(Organize::getId).collect(Collectors.toList());
            }else{
                organizeIds.clear();
            }
            organizeIds.add(inputDTO.getOrganizeId());
            inputDTO.setOrganizeIds(organizeIds);
        }
        inputDTO.setOrganizeIds(organizeIds);

        return inputDTO;
    }

    @Override
    public VenterpriseDeviceStatisticsOutput getDeviceStatistics() {
        VenterpriseDeviceStatisticsOutput dto = null;
        List<VenterpriseDeviceStatistics> list = venterpriseDeviceStatisticsRepository.findAll();
        if (!CollectionUtils.isEmpty(list)) {
            dto = new VenterpriseDeviceStatisticsOutput();
            BeanUtils.copyProperties(list.get(0), dto);
        }
        return dto;
    }

}
